﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using StudentMgmt.SQLServerDAL;
using StudentMgmt.Model;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using System.Configuration;

namespace StudentMgmt
{
    public partial class frmNewStudent : Form
    {
        public frmNewStudent()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        /*双击窗口进入，窗口的初始化函数*/
        private void frmNewStudent_Load(object sender, EventArgs e)
        {        
            Department department = new Department();
            IList<DepartmentInfo> depts = department.GetDepartments();
            this.cmbDepartment.DisplayMember = "Department_Name";
            this.cmbDepartment.ValueMember = "Department_Id";
            this.cmbDepartment.DataSource = depts.ToList();

            Classes oclass = new Classes();
            IList<ClassInfo> classes = oclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());
            this.cmbClasses.DisplayMember = "ClassName";
            this.cmbClasses.ValueMember = "ClassId";
            this.cmbClasses.DataSource = classes.ToList();

            string strPath = ConfigurationManager.AppSettings["CityXMLPath"].ToString();
            strPath = strPath + "citys.xml";
            XDocument doc;
            try
            {
                doc = XDocument.Load(strPath);//Create a new XDocument from a file 
            }
            catch (Exception)
            {

                throw;
            }

            List<string> citys = new List<string>();
            var query = from t in doc.Descendants("province") //DescendantNodes() 按文档顺序返回此文档或元素的子代节点的集合。
                        select new
                        {
                            City = t.Attribute("name").Value
                        };

            if (query.Count() > 0)
            {
                foreach (var P in query)
                {
                    citys.Add(P.City.ToString());
                }
            }

            this.cmbProvince.DisplayMember = "City";
            this.cmbProvince.ValueMember = "City";
            this.cmbProvince.DataSource = citys;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void cmbDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            Classes oclass = new Classes();
            IList<ClassInfo> classes = oclass.GetClassesByDeptId(this.cmbDepartment.SelectedValue.ToString());

            this.cmbClasses.DisplayMember = "ClassName";
            this.cmbClasses.ValueMember = "ClassId";
            this.cmbClasses.DataSource = classes.ToList();
        }

        private void btnGetNextStuNo_Click(object sender, EventArgs e)
        {
            Student student = new Student();
            string strMaxStuNo = student.GetCurrentMaxStuNo(this.cmbDepartment.SelectedValue.ToString());
            this.txtNextStuNo.Text = GetNextStuNo(strMaxStuNo);
        }

        private string GetNextStuNo(string currentStuNo)
        {
            string pattern = "^0+";
            string StuPrefix = currentStuNo.Substring(0, 3);
            string StuNo = currentStuNo.Substring(3, 3);
            StuNo = StuNo.Replace(Regex.Match(StuNo, pattern).Value, string.Empty);
            int iNextValue = int.Parse(StuNo) + 1;
            string nextStuNo = iNextValue.ToString().PadLeft(3, '0');

            return StuPrefix + nextStuNo;
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.StudentNo = this.txtNextStuNo.Text.Trim();
            studentInfo.StudentName = this.txtStudentName.Text.Trim();
            studentInfo.Sex = this.rdbFemale.Checked ? "F" : "M";
            studentInfo.Birthday = this.dtPickBirthday.Value.ToShortDateString();
            studentInfo.ClassId = this.cmbClasses.SelectedValue.ToString();
            studentInfo.NativePlace = this.cmbProvince.SelectedValue.ToString();

            Student student = new Student();

            try
            {
                student.InsertStudent(studentInfo);
                MessageBox.Show("新增成功!");
                return;
            }
            catch (Exception)
            {
                MessageBox.Show("新增失败!");
                return;
            }
        }
    }
}
